<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="" prop="account">
        <el-input
          v-model="queryParams.account"
          placeholder="请输入账号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入商户全称或个人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="platformAgentId">
        <el-input
          v-model="queryParams.platformAgentId"
          placeholder="请输入代理商ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="platformAgentName">
        <el-input
          v-model="queryParams.platformAgentName"
          placeholder="请输入代理商名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:receivers:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
<!--        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:receivers:edit']"
        >修改</el-button>-->
      </el-col>
<!--      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:receivers:remove']"
        >删除</el-button>
      </el-col>-->
<!--      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:receivers:export']"
        >导出</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="receiversList" @selection-change="handleSelectionChange" highlight-current-row>
<!--      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />-->
      <el-table-column label="账号" align="center" prop="account" />
      <el-table-column label="商户全称或个人姓名" align="center" prop="name" />
      <el-table-column label="关系" align="center" prop="relationType">
        <template slot-scope="scope">
          {{getLabelByValue(relationTypeList,scope.row.relationType)}}
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="scope">
          {{getLabelByValue(typeList,scope.row.type)}}
        </template>
      </el-table-column>
      <el-table-column label="代理商ID" align="center" prop="platformAgentId" />
      <el-table-column label="代理商名称" align="center" prop="platformAgentName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            :class="['text-button', { 'active': active===scope.$index + 'handleUpdate' }]"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row),active=scope.$index + 'handleUpdate'"
            v-hasPermi="['business:receivers:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:receivers:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改分账接收方列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="类型" prop="type">
          <el-select
            allow-create
            default-first-option
            v-model="form.type"
            placeholder="请选代理商"
            clearable
            @change="form.type==='MERCHANT_ID'?accountLabel='商户号':accountLabel='个人openId'"
            size="small">
            <el-option
              v-for="area in typeList"
              :key="area.value"
              :label="area.label"
              :value="area.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="accountLabel" prop="account">
          <el-input v-model.trim="form.account" :placeholder="`请输入${accountLabel}`"/>
        </el-form-item>
        <el-form-item label="商户全称或个人姓名" prop="name">
          <el-input v-model.trim="form.name" placeholder="请输入商户全称或个人姓名"/>
        </el-form-item>

        <el-form-item label="商户与接收方关系" prop="relationType">
          <el-select
            allow-create
            default-first-option
            v-model="form.relationType"
            placeholder="商户与接收方关系"
            clearable
            size="small">
            <el-option
              v-for="area in relationTypeList"
              :key="area.value"
              :label="area.label"
              :value="area.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="代理商名称" prop="platformAgentName">
          <el-select
            allow-create
            default-first-option
            v-model="form.platformAgentName"
            placeholder="代理商名称"
            @change="changeAgent"
            clearable
            size="small">
            <el-option
              v-for="area in agentList"
              :key="area.id"
              :label="area.agentName"
              :value="area.agentName"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listReceivers, getReceivers, delReceivers, addReceivers, updateReceivers } from "@/api/business/receivers";
import {getAgentAll} from "@/api/platform/agent"
const typeList = [
  {
    label: '商户号',
    value: 'MERCHANT_ID'
  }, {
    label: '个人openid',
    value: 'PERSONAL_OPENID'
  }
]
const relationTypeList = [
  {
    value: 'STORE',
    label: '门店'
  },
  {
    value: 'STAFF',
    label: '员工'
  },
  {
    value: 'STORE_OWNER',
    label: '店主'
  },
  {
    value: 'PARTNER',
    label: '合作伙伴'
  },
  {
    value: 'HEADQUARTER',
    label: '总部'
  },
  {
    value: 'BRAND',
    label: '品牌方'
  },
  {
    value: 'DISTRIBUTOR',
    label: '分销商'
  },
  {
    value: 'USER',
    label: '用户'
  },
  {
    value: 'SUPPLIER',
    label: '供应商'
  }
]
export default {
  name: "Receivers",
  data() {
    return {
      active: null,
      typeList,
      relationTypeList,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 分账接收方列表表格数据
      receiversList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        account: null,
        name: null,
        relationType: null,
        type: null,
        platformAgentId: null,
        platformAgentName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        type: { required: true, message: '类型是必填项' },
        account: { required: true, message: '账号是必填项' },
        name: { required: true, message: '名称是必填项' },
        relationType: { required: true, message: '关系类型是必填项'},
        platformAgentName: { required: true, message: '平台代理名称是必填项' }
      },
      accountLabel:'商户号',
      agentList:[],
    };
  },
  created() {
    this.getList();
    this.getAgentAll()
  },
  methods: {
    changeAgent(el){
      this.$set(this.form,'platformAgentId',this.getValueByLabel(this.agentList,el))
    },
    getValueByLabel(entities, label) {
      for (let i = 0; i < entities.length; i++) {
        if (entities[i].agentName === label) {
          return entities[i].id
        }
      }
    },
    getLabelByValue(entities, value) {
      for (let i = 0; i < entities.length; i++) {
        if (entities[i].value === value) {
          return entities[i].label
        }
      }
    },
    async getAgentAll(){
      let res = await getAgentAll()
      this.agentList = res.rows
    },
    /** 查询分账接收方列表列表 */
    getList() {
      this.loading = true;
      listReceivers(this.queryParams).then(response => {
        this.receiversList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        account: null,
        name: null,
        relationType: null,
        type: null,
        platformAgentId: null,
        platformAgentName: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加分账接收方列表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getReceivers(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改分账接收方列表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateReceivers(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addReceivers(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除分账接收方列表编号为"' + ids + '"的数据项？').then(function() {
        return delReceivers(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/receivers/export', {
        ...this.queryParams
      }, `receivers_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
